package cn.huiyunche.driver.datasync.controller;

import cn.huiyunche.base.service.interfaces.DWaybillService;
import cn.huiyunche.base.service.model.TmsOrder;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.driver.service.FetchOtmOrder;
import cn.huiyunche.driver.service.OrderService;
import com.beust.jcommander.internal.Lists;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by zhaoguixin on 2018/8/1.
 */
@Controller
@RequestMapping("/otmOrderSync")
public class OtmOrderSyncController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OtmOrderSyncController.class);

    @Autowired
    private FetchOtmOrder fetchOtmOrder = null;

    @Autowired
    private OrderService orderService = null;

    @Autowired
    private DWaybillService dWaybillService= null;

    /**
     * 按时间抓取最新订单号
     * @param lastTime
     * @return
     **/
    @RequestMapping(value = "/grabLastOrder", method = RequestMethod.GET)
    public @ResponseBody
    Result<List<String>> grabLastOrder(@RequestParam("lastTime")String lastTime) {
        Result<List<String>> result = new Result<>(true, "查询成功");

        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime last = DateTime.parse(lastTime,format);

        List<String> orderNos = fetchOtmOrder.fetchPendingOrderNo(last.toDate());

        if(null==orderNos||orderNos.size()==0) {
            result.setMessage("无订单");
            return result;
        }

        TmsOrder tmsOrder;
        List<TmsOrder> tmsOrders = Lists.newArrayList();
        for (String orderNo:orderNos) {
            tmsOrder = fetchOtmOrder.fetchOtmOrderByOrderNo(orderNo);
            tmsOrders.add(tmsOrder);

            try {
                orderService.handleTmsOrders(tmsOrders);
            } catch (Exception e) {
                LOGGER.error("OrderServiceImpl.handleTmsOrders is error orderNo:{},e:{}.", orderNo,e);
            }
            tmsOrders.clear();
        }

        result.setData(orderNos);
        return result;
    }

    /**
     * 从TMS抓取单条订单
     * @param orderCode
     * @return
     **/
    @RequestMapping(value = "/grabSingleOrder", method = RequestMethod.GET)
    public
    @ResponseBody
    Result<Object> grabSingleOrder(@RequestParam("orderCode")String orderCode) {
        Result<Object> result = new Result<>(true, "查询成功");

        TmsOrder tmsOrder = fetchOtmOrder.fetchOtmOrderByOrderNo(orderCode);
        if(null!=tmsOrder){
            List<TmsOrder> tmsOrders = Lists.newArrayList();
            tmsOrders.add(tmsOrder);
            orderService.handleTmsOrders(tmsOrders);
            result.setData(tmsOrders);
        }else {
            result.setMessage("查无数据");
        }
        return result;
    }

//    /**
//     * 重新计算价格推送tms
//     * @param waybillId
//     * @return
//     **/
//    @RequestMapping(value = "/accountingPrice", method = RequestMethod.GET)
//    public
//    @ResponseBody
//    Result<Object> accountingPrice(@RequestParam("waybillId")String waybillId) {
//        Result<Object> result = new Result<>(true, "计算成功");
//        try{
//            dWaybillService.recalculateAndUpdate(waybillId);
//        }catch (Exception e){
//            result.setMessage(e.getMessage());
//        }
//        return result;
//    }
}
